package com.profgj.profgj.utils;

import com.profgj.profgj.vo.ShortMessage;
import com.profgj.profgj.vo.TaskMgr;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * (定时短信任务)
 *
 * @author 杨帅
 * @since 2021-06-22 11:09:35
 */

@Component
@Api(tags = {"定时短信任务"}, description = "定时发送短信")
public class TaskService {

    /**
     * Http请求对象
     */
    @Resource
    private RestTemplate restTemplate;

    /**
     * 将application.properties中的值注入到变量中
     */
    @Value("${system.shortmessage.address}")
    private String shortMessageAddress = null;

    /**
     * 发送短信的用户名
     */
    @Value("${system.shortmessage.username}")
    private String username = null;

    /**
     * 发送短信的密码
     */
    @Value("${system.shortmessage.password}")
    private String password = null;

    /**
     * 发送短信的签名
     */
    @Value("${system.shortmessage.signName}")
    private String signName = null;


    /**
     * 短信发送
     * @param taskMgr   发送内容存放对象
     * @return
     */
    public ShortMessage sendMessage(TaskMgr taskMgr) {


        // sign的日期时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        // 取当前系统时间
        Date currentDate = new Date();

        // 当前系统时间格式化成字符串20210704125617
        String timestampstr = sdf.format(currentDate);

        Long timestamp = currentDate.getTime() / 1000;

        // 算出sign字符串
        String sign = DigestUtils.md5DigestAsHex((username + password + timestamp).getBytes()).toLowerCase();
        System.out.println("sign原字符串:" + username + password + timestamp);
        System.out.println("signMD5:" + sign);

        String sendTimestr = "";
        // 请求的URL地址存储
        String requestUrl = "";
        // 发送的时间存储
        Date sendTime = null;

        // 拼接发送地址
        requestUrl = this.shortMessageAddress + "timestamp=" +timestamp + "&sign=" + sign + "&mobile=" +
                taskMgr.getPhoneNumber() + "&content=" +  signName +  taskMgr.getContent() + "&sendTime=" + "&extno=";

        // 发起请求返回xml数据
        String body = restTemplate.getForEntity(requestUrl, String.class).getBody();

        // 解析XML结果
        ShortMessage shortMessage = parserXML(body);

        System.out.println(shortMessage);

        // 返回
        return shortMessage;
    }


    /**
     * 取XML结果信息
     * @param body  包含XML的 字符串
     * @return  返回结果信息对象
     */
    private ShortMessage parserXML(String body) {

        // 结果字符串的正则
        String returnstatus_Pattern = ">([a-z,A-Z]+)<";
        // 结果消息正则
        String message_Pattern = ">([a-z,\\u4e00-\\u9fa5]+)</";
        // 结果余额正则
        String remainpoint_Pattern = "t>([0-9]+)</r";
        // 结果成功发送数正则
        String uccessCounts_Pattern = "s>([0-9]+)</s";

        // 短信发送结果对象
        ShortMessage shortMessage = new ShortMessage();

        // 分别创建匹配器
        Matcher returnstatus_m = Pattern.compile(returnstatus_Pattern).matcher(body);
        Matcher message_m = Pattern.compile(message_Pattern).matcher(body);
        Matcher remainpoint_m = Pattern.compile(remainpoint_Pattern).matcher(body);
        Matcher uccessCounts_m = Pattern.compile(uccessCounts_Pattern).matcher(body);

        // 将找到的结果 设置到对象内
        if(returnstatus_m.find()) {
            shortMessage.setReturnstatus(returnstatus_m.group(1));
        }
        if (message_m.find()) {
            shortMessage.setMessage(message_m.group(1));
        }
        if (remainpoint_m.find()) {
            shortMessage.setRemainpoint(remainpoint_m.group(1));
        }
        if (uccessCounts_m.find()) {
            shortMessage.setUccessCounts(uccessCounts_m.group(1));
        }

        // 返回
        return shortMessage;
    }

}
